using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Colorprj.Masters;
using Colorprj.Common;

namespace Colorprj
{
    public partial class frmItemMaster : Form
    {
        private SubProductManager spm = new SubProductManager();
        private TabPage prdTab;
        private TabPage subPrdTab ;
        enum MODE {
            AddGroup,     
            AddItem      ,
            EditGroup,     
            EditItem ,
            DelGroup,
            DelItem 

        }


        private MODE OpMode = MODE.AddGroup ;
        public frmItemMaster()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
         
        }

        private void tabPage4_Click(object sender, EventArgs e)
        {

        }
        private void fillTree() {

            this.treeView1.Nodes.Clear() ;
            DataSet customerList = spm.GetAllProducts();
            string preGrp = "";
            TreeNode mainNode = new TreeNode();
            for (int i = 0; i <= customerList.Tables[0].Rows.Count - 1; i++)
            {
                // MessageBox.Show(customerList.Tables[0].Rows[i].ItemArray[0] + " -- " + customerList.Tables[0].Rows[i].ItemArray[1]);
                if (!preGrp.Equals(customerList.Tables[0].Rows[i].ItemArray[0].ToString()))
                {


                    mainNode = new TreeNode();


                    mainNode.Name = customerList.Tables[0].Rows[i].ItemArray[0].ToString();

                    mainNode.Text = customerList.Tables[0].Rows[i].ItemArray[1].ToString();
                    TreeNode cNode = new TreeNode();
                    cNode.Name = customerList.Tables[0].Rows[i].ItemArray[2].ToString();

                    cNode.Text = customerList.Tables[0].Rows[i].ItemArray[3].ToString();
                    mainNode.Nodes.Add(cNode);
                    this.treeView1.Nodes.Add(mainNode);
                }
                else
                {
                    TreeNode cNode = new TreeNode();
                    cNode.Name = customerList.Tables[0].Rows[i].ItemArray[2].ToString();

                    cNode.Text = customerList.Tables[0].Rows[i].ItemArray[3].ToString();
                    mainNode.Nodes.Add(cNode);
                }
                preGrp = customerList.Tables[0].Rows[i].ItemArray[0].ToString();

            }
        }

        private void frmItemMaster_Load(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                subPrdTab = tabControl2.TabPages[0];
                prdTab = tabControl2.TabPages[1];
                tabControl2.SelectTab(1);
                
                tabControl2.TabPages.Remove(tabControl2.TabPages[0]); 

                btnDelete.Enabled = false;
                //btnEdit.Enabled = false;
                btnNewItm.Enabled = false;
                btnSave.Enabled = false;
                //btnUndo.Enabled = false;
                //btnDelete.Enabled = false; 


                fillTree();


                // this.DisplayResults(this.button1.Text, customerList);
            }
            catch (Exception ex)
            {
                //DisplayErrors(ex);
            }
            finally
            {
                Cursor = Cursors.Arrow;
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {

            if (e.Node.Level.ToString().Equals("0"))            {
                tabControl2.TabPages.Clear();
                tabControl2.TabPages.Add(prdTab);   
                tabControl2.SelectTab(0);
                txtGrpProductId.Text = treeView1.SelectedNode.Name;
                txtGrpProductName.Text = treeView1.SelectedNode.Text;
                btnDelete.Enabled = true;
                btnSave.Enabled = true; 
               // btnEdit.Enabled = false;
                btnNewGrp.Enabled = true;
                btnNewItm.Enabled = true;
                OpMode = MODE.EditGroup; 
            } else {
                tabControl2.TabPages.Clear();
                tabControl2.TabPages.Add(subPrdTab);  
                tabControl2.SelectTab(0);
                txtProductId.Text = treeView1.SelectedNode.Parent.Name;
                txtProductName.Text = treeView1.SelectedNode.Parent.Text;
                int subprdId = Convert.ToInt16(e.Node.Name);

                SubProduct spEntity = spm.GetSubProductById(subprdId);
                txtName.Text  = spEntity.SubProductName;
                txtPRate.Text  = spEntity.PurRate.ToString() ;
                txtSRate.Text  = spEntity.SaleRate.ToString() ;
                txOPBal .Text  = spEntity.OpBal.ToString()  ;
                btnSave.Enabled = true; 
                btnDelete.Enabled = true;
                //btnEdit.Enabled = true;
                btnNewGrp.Enabled = false;
                btnNewItm.Enabled = false;
                OpMode = MODE.EditItem; 
            }

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void tabPage5_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
             TreeNode cNode = new TreeNode();
            cNode.Name ="newGroup";
            cNode.Text  = "new Group";
            treeView1.Nodes.Add(cNode);
            txtGrpProductName.Text = "newGroup";
            txtGrpProductId.Text = "";
            btnSave.Enabled = true;
            OpMode = MODE.AddGroup;
        }

        private void button3_Click(object sender, EventArgs e)
        {


            try
            {


            if (OpMode == MODE.AddGroup)
            {

                spm.AddProduct(txtGrpProductName.Text);
                fillTree();
            }
            if (OpMode == MODE.EditItem ) {
                SubProduct spEntity = getSubProductDataFromForm();
                spm.UpdSubProduct(spEntity);
                fillTree();
            
            }
            if (OpMode == MODE.EditGroup) {
               Product spEntity = getProductDataFromForm();
               spm.UpdProduct(spEntity);
               fillTree();
            
            }
            if (OpMode == MODE.AddItem) {

                SubProduct spEntity = getSubProductDataFromForm();
                spm.AddSubProduct(spEntity);
                fillTree();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message)  ;
        }

 }

        private Product getProductDataFromForm()
        {

            Product spEntity = new Product();
            spEntity.ProductId = Convert.ToInt16(txtGrpProductId.Text);
            spEntity.ProductName = txtGrpProductName.Text;
            return spEntity;
        }


        private SubProduct getSubProductDataFromForm() {

            SubProduct spEntity = new SubProduct();
            spEntity.ProductId = Convert.ToInt16(txtProductId.Text);
            spEntity.OpBal = Convert.ToInt16(txOPBal.Text);
            spEntity.SubProductName = txtName.Text;
            spEntity.SaleRate = Utility.getDblVal(txtSRate.Text);
            spEntity.PurRate = Utility.getDblVal(txtPRate.Text);
            spEntity.Units = txtUnits.Text;
            spEntity.SubProductId = Convert.ToInt16(treeView1.SelectedNode.Name);   
            return spEntity;
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

      

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnDelete.Enabled = false ;
            //btnEdit.Enabled = false ;
            btnSave.Enabled = true;
            //btnUndo.Enabled = true; 
        }

        private void btnNewItm_Click(object sender, EventArgs e)
        {
            int productId= Convert.ToInt16(treeView1.SelectedNode.Name);
            //productName = Convert.ToInt16();
            tabControl2.TabPages.Clear();
            tabControl2.TabPages.Add(subPrdTab);  
            tabControl2.SelectTab(0);
            txtProductId.Text = productId.ToString();
            txtProductName.Text = treeView1.SelectedNode.Text;
            btnDelete.Enabled = false;
            //btnEdit.Enabled = false;
            btnSave.Enabled = true;
            //btnUndo.Enabled = true;
            btnNewGrp.Enabled = false;
            btnNewItm.Enabled = false;
            OpMode = MODE.AddItem;
            txOPBal.Text = "0";
            txtSRate.Text = "0";
            txtPRate.Text = "0";
            txtName.Text = txtProductName.Text + " Product 1";

        }

        private void label12_Enter(object sender, EventArgs e)
        {

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try {
            if (OpMode == MODE.EditItem)
            {
                SubProduct spEntity = getSubProductDataFromForm();
                spm.DelSubProduct(spEntity.SubProductId);
                fillTree();

            }
            if (OpMode == MODE.EditGroup)
            {
                MessageBox.Show("Product Can not be deleted !");  
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        }
        private void NumericKeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            TextBox tb = (TextBox)sender;


            if (!(char.IsDigit(e.KeyChar) | char.IsControl(e.KeyChar) | (e.KeyChar.Equals('.') & tb.Text.IndexOf(".") < 0)))
            {
                e.Handled = true;

            }


            if ((tb.SelectionStart > tb.Text.Length - 2 & tb.Text.IndexOf(".") >= 0 & tb.Text.IndexOf(".") + 3 == tb.Text.Length))
            {
                e.Handled = true;

            }


            if (e.Handled == false & tb.Text.Length == 0)
            {
                tb.Text = "" + e.KeyChar;

                e.Handled = true;

                tb.SelectionStart = 2;

            }

        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // frmItemMaster
            // 
            this.ClientSize = new System.Drawing.Size(500, 292);
            this.Name = "frmItemMaster";
            this.ResumeLayout(false);

        }
      
    }
}